package org.amphiprion.rfreplayanalyser.ui.lapanalyzer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.amphiprion.rfreplayanalyser.util.TimingUtil;

@SuppressWarnings("serial")
public class LapPanel extends JPanel {
	private LapItem lapItem;
	private LapProvider lapProvider;

	/**
	 * Create the panel.
	 */
	public LapPanel(final LapProvider lapProvider, final LapItem lapItem) {
		this.lapItem = lapItem;
		this.lapProvider = lapProvider;

		setLayout(new BorderLayout(0, 0));

		final JButton btnNewButton = new JButton("...");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Color color = JColorChooser.showDialog(LapPanel.this, "", lapItem.color);
				if (color != null) {
					lapItem.color = color;
					lapProvider.lapItemUpdated(lapItem);
					btnNewButton.setBackground(lapItem.color);
				}
			}
		});
		btnNewButton.setBackground(lapItem.color);
		add(btnNewButton, BorderLayout.WEST);

		JLabel lblLapInfo = new JLabel("<html>" + lapProvider.getDriverName(lapItem.driverId) + " - Lap " + lapItem.lap + " [" + TimingUtil.getTimeAsLaptimeString(lapItem.laptime) + "]<br/>S1=" + TimingUtil.getTimeAsLaptimeString(lapItem.sector1) + " - S2="
				+ TimingUtil.getTimeAsLaptimeString(lapItem.sector2) + " - S3=" + TimingUtil.getTimeAsLaptimeString(lapItem.sector3) + "<html>");
		add(lblLapInfo, BorderLayout.CENTER);

		JButton btnNewButton_1 = new JButton("X");
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getParent().remove(LapPanel.this);
				lapProvider.lapItemRemoved(lapItem);
			}
		});
		add(btnNewButton_1, BorderLayout.EAST);

		addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				getParent().invalidate();
				if (lapProvider.isLapItemSelected(lapItem)) {
					lapProvider.lapItemSelected(null, false); // unselect
				} else {
					lapProvider.lapItemSelected(lapItem, true);
				}
				getParent().validate();
				getParent().repaint();
			}
		});
	}

	@Override
	public void paint(Graphics g) {
		if (lapProvider.isLapItemSelected(lapItem)) {
			setBackground(UIManager.getColor("List.selectionBackground"));
		} else {
			setBackground(UIManager.getColor("Panel.background"));
		}
		super.paint(g);
	}

}
